package com.TP.www.service.impl;

import com.TP.www.dao.impl.GoodDAO;
import com.TP.www.po.Good;
import com.TP.www.service.GoodPort;

import static com.TP.www.constant.Constant.*;

/**
 * @author 邱彦祥
 * 商品功能实现类
 */
public class GoodImpl implements GoodPort {
    GoodDAO goodDAO = new GoodDAO();
    /**
     * 增加商品
     * @param storeId 店铺id
     */
    @Override
    public void addGood(int storeId){
        while(true){
            try{
                String goodName;
                String priceString;
                double price;
                double priceMax = 1000;
                System.out.println("请输入上架商品名:");
                goodName = SYS_SC.nextLine();
                System.out.println("请输入单价(不能大于一千): ");
                priceString = SYS_SC.nextLine();
                while (Double.parseDouble(priceString) <= 0 || Double.parseDouble(priceString) > priceMax){
                    System.out.println("输入有误！请重新输入...");
                    priceString = SYS_SC.nextLine();
                }
                price = Double.parseDouble(priceString);
                if(goodDAO.executeUpdate(CREAT_GOODS_BY_STORE_ID,goodName,price,storeId) > 0){
                    System.out.println("添加成功！");
                }else {
                    System.out.println("添加失败...");
                }
                System.out.println("按任意键返回...");
                SYS_SC.nextLine();
                break;
            }catch (NumberFormatException e){
                System.out.println("输入有误！请重新输入...");
            }
        }
    }

    /**
     * 修改商品
     * @param good 商品
     * @param storeId 店铺id
     */
    @Override
    public  void modifyGood(Good good, int storeId){
        while(true){
            try{
                System.out.println("*******商品详情******");
                System.out.println("商品名: " + good.getGoodName() + " 单价: " + good.getPrice());
                System.out.println("*******************");
                String goodName;
                String priceString;
                double price;
                double priceMax = 1000;
                System.out.println("请输入修改后商品名:");
                goodName = SYS_SC.nextLine();
                System.out.println("请输入修改后的单价(不能大于一千): ");
                priceString = SYS_SC.nextLine();
                while (Double.parseDouble(priceString) <= 0 || Double.parseDouble(priceString) > priceMax){
                    System.out.println("输入有误！请重新输入...");
                    priceString = SYS_SC.nextLine();
                }
                price = Double.parseDouble(priceString);
                if(goodDAO.executeUpdate(UPDATE_GOODS_BY_GOODE_ID,goodName,price,good.getId(),storeId) > 0){
                    System.out.println("修改成功！");
                }else {
                    System.out.println("修改失败...");
                }
                System.out.println("按任意键返回...");
                SYS_SC.nextLine();
                break;
            }catch (NumberFormatException e){
                System.out.println("输入有误！请重新输入...");
            }
        }

    }

    /**
     * 下架商品
     * @param good 商品
     * @param storeId 店铺id
     */
    @Override
    public void offGood(Good good, int storeId){
        int off = -1;
        if (goodDAO.executeUpdate(OFF_GOODS_BY_STORE_ID,off,good.getId(),storeId) > 0){
            System.out.println("下架成功!");
        }else {
            System.out.println("下架失败...");
        }
        System.out.println("按任意键返回...");
        SYS_SC.nextLine();
    }
}
